package com.mall.user.services;


import com.mall.user.RegisterService;
import com.mall.user.converter.MemberConverter;
import com.mall.user.dal.entitys.Member;
import com.mall.user.dal.entitys.UserVerify;
import com.mall.user.dal.persistence.MemberMapper;
import com.mall.user.dal.persistence.UserVerifyMapper;
import com.mall.user.dto.UserRegisterRequest;
import com.mall.user.dto.UserVerifyRequest;
import com.mall.user.utils.AESUtil;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import tk.mybatis.mapper.entity.Example;

import java.sql.Date;
import java.util.List;

@Component
@Service
public class RegisterServiceImpl implements RegisterService {
    @Autowired
    MemberConverter memberConverter;

    @Autowired
    MemberMapper memberMapper;

    @Autowired
    UserVerifyMapper verifyMapper;

    @Autowired
    EmailServiceImpl emailService;


    @Override
    public boolean register(UserRegisterRequest registerRequest) {
        Example example = new Example(Member.class);
        example.createCriteria().andEqualTo("username", registerRequest.getUserName());
        List<Member> checkUsername = memberMapper.selectByExample(example);

        example = new Example(Member.class);
        example.createCriteria().andEqualTo("email", registerRequest.getEmail());
        List<Member> checkEmail = memberMapper.selectByExample(example);
        //如果用户名或邮箱以存在则返回false
        if (checkUsername.size() != 0 || checkEmail.size() != 0) {
            return false;
        } else {
            Member member = memberConverter.userRegisterRequest2Member(registerRequest);
            //设置创建时间
            member.setCreated(new Date(System.currentTimeMillis()));
            member.setUpdated(new Date(System.currentTimeMillis()));
            //对密码进行加密
            String password = new AESUtil(member.getPassword()).encrypt().toLowerCase();
            member.setPassword(password);
            //设置验证状态
            member.setIsVerified("N");
            memberMapper.insert(member);

            //插入用户验证信息
            UserVerify userVerify = new UserVerify();
            userVerify.setUsername(member.getUsername());
            userVerify.setUuid(member.getPassword());
            userVerify.setIsVerify("N");
            userVerify.setIsExpire("N");
            userVerify.setRegisterDate(member.getCreated());

            verifyMapper.insert(userVerify);


            //注册完成发送邮件
            emailService.sendEmail(member);

            return true;
        }

    }

    @Override
    public boolean verify(UserVerifyRequest verifyRequest) {
        Example verifyExample = new Example(UserVerify.class);
        verifyExample.createCriteria().andEqualTo("uuid",verifyRequest.getUid())
                                .andEqualTo("username",verifyRequest.getUsername());
        List<UserVerify> userVerifies = verifyMapper.selectByExample(verifyExample);

        Example memberExample = new Example(Member.class);
        memberExample.createCriteria().andEqualTo("username",verifyRequest.getUsername());
        List<Member> members = memberMapper.selectByExample(memberExample);
        if(userVerifies.size() == 0){
            return false;
        }else {
            userVerifies.get(0).setIsVerify("Y");
            verifyMapper.updateByPrimaryKey(userVerifies.get(0));
            members.get(0).setIsVerified("Y");
            memberMapper.updateByPrimaryKey(members.get(0));
            return true;
        }
    }
}
